/*
 * Copyright 2019 The ZuSmart Project
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zusmart.base.logging.support;

import static com.zusmart.base.logging.support.AbstractLogger.EXCEPTION_MESSAGE;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;

import com.zusmart.base.logging.LoggerLevel;
import com.zusmart.base.logging.format.Formatter;
import com.zusmart.base.logging.format.FormatterTuple;

/**
 * @author Administrator
 *
 */
public class Log4J2Logger extends ExtendedLoggerWrapper implements com.zusmart.base.logging.Logger {

	private static final long serialVersionUID = 5485418394879791397L;

	public Log4J2Logger(Logger logger) {
		super((ExtendedLogger) logger, logger.getName(), logger.getMessageFactory());
	}

	@Override
	public void trace(Throwable t) {
		this.log(Level.TRACE, EXCEPTION_MESSAGE, t);
	}

	@Override
	public void debug(Throwable t) {
		this.log(Level.DEBUG, EXCEPTION_MESSAGE, t);
	}

	@Override
	public void info(Throwable t) {
		this.log(Level.INFO, EXCEPTION_MESSAGE, t);
	}

	@Override
	public void warn(Throwable t) {
		this.log(Level.WARN, EXCEPTION_MESSAGE, t);
	}

	@Override
	public void error(Throwable t) {
		this.log(Level.ERROR, EXCEPTION_MESSAGE, t);
	}

	@Override
	public boolean isEnabled(LoggerLevel level) {
		return this.isEnabled(toLevel(level));
	}

	@Override
	public void trace(String msg, Object arg) {
		FormatterTuple tuple = Formatter.format(msg, arg);
		this.logger.trace(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void trace(String msg, Object argA, Object argB) {
		FormatterTuple tuple = Formatter.format(msg, argA, argB);
		this.logger.trace(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void debug(String msg, Object arg) {
		FormatterTuple tuple = Formatter.format(msg, arg);
		this.logger.trace(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void debug(String msg, Object argA, Object argB) {
		FormatterTuple tuple = Formatter.format(msg, argA, argB);
		this.logger.debug(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void info(String msg, Object arg) {
		FormatterTuple tuple = Formatter.format(msg, arg);
		this.logger.info(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void info(String msg, Object argA, Object argB) {
		FormatterTuple tuple = Formatter.format(msg, argA, argB);
		this.logger.info(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void warn(String msg, Object arg) {
		FormatterTuple tuple = Formatter.format(msg, arg);
		this.logger.warn(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void warn(String msg, Object argA, Object argB) {
		FormatterTuple tuple = Formatter.format(msg, argA, argB);
		this.logger.warn(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void error(String msg, Object arg) {
		FormatterTuple tuple = Formatter.format(msg, arg);
		this.logger.error(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void error(String msg, Object argA, Object argB) {
		FormatterTuple tuple = Formatter.format(msg, argA, argB);
		this.logger.error(tuple.getMessage(), tuple.getThrowable());
	}

	@Override
	public void log(LoggerLevel level, String msg) {
		this.log(toLevel(level), msg);
	}

	@Override
	public void log(LoggerLevel level, String format, Object arg) {
		this.log(toLevel(level), format, arg);
	}

	@Override
	public void log(LoggerLevel level, String format, Object argA, Object argB) {
		this.log(toLevel(level), format, argA, argB);
	}

	@Override
	public void log(LoggerLevel level, String format, Object... arguments) {
		this.log(toLevel(level), format, arguments);
	}

	@Override
	public void log(LoggerLevel level, String msg, Throwable t) {
		this.log(toLevel(level), msg, t);
	}

	@Override
	public void log(LoggerLevel level, Throwable t) {
		this.log(toLevel(level), EXCEPTION_MESSAGE, t);
	}

	private static Level toLevel(LoggerLevel level) {
		if (null == level) {
			throw new Error();
		}
		if (level == LoggerLevel.TRACE) {
			return Level.TRACE;
		} else if (level == LoggerLevel.DEBUG) {
			return Level.DEBUG;
		} else if (level == LoggerLevel.INFO) {
			return Level.INFO;
		} else if (level == LoggerLevel.WARN) {
			return Level.WARN;
		} else if (level == LoggerLevel.ERROR) {
			return Level.ERROR;
		}
		throw new Error();
	}

}